package server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;

public class ServerReaderThread {
    private Socket socket;
    public ServerReaderThread(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            while (true) {
                int messageType = dis.readInt();
                switch (messageType) {
                    case 1:
                        String nickname = dis.readUTF();
                        //把这个登录成功的客户端socket存到在线集合中
                        Server.onLineSockets.put(socket, nickname);
                        //更新在线用户列表
                        updateClientOnLineList();
                        break;
                    case 2:
                        String message = dis.readUTF();
                        sendMsgToAll(message);
                        break;
                    case 3:

                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("客户端下线了");
            Server.onLineSockets.remove(socket);
            updateClientOnLineList();
        }
    }

    private void sendMsgToAll(String message) {
        //把消息转发给全部在线用户
        //拼装好这个消息，然后转发给全部在线用户
        StringBuilder sb = new StringBuilder();
        String name = Server.onLineSockets.get(socket);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EEE a");
        String nowStr = dtf.format(now);
        String msgResult = sb.append(name)
                .append("[")
                .append(nowStr)
                .append("] ")
                .append("/r/n")
                .append(message)
                .append("/r/n")
                .toString();
        for (Socket client : Server.onLineSockets.keySet()) {
            try {
                DataOutputStream dos = new DataOutputStream(client.getOutputStream());
                dos.writeInt(2);
                dos.writeUTF(msgResult);
                dos.flush();
            } catch (Exception e) {
                e.printStackTrace();

            }
        }


        Collection<Socket> onLineSockets = Server.onLineSockets.keySet();
        for (Socket client : onLineSockets) {
            try {
                DataOutputStream dos = new DataOutputStream(client.getOutputStream());
                dos.writeInt(2);
                dos.writeUTF(message);
                dos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void updateClientOnLineList() {
        //拿到全部在线用户名称，把这些名称转发给在线线程管道
        Collection<String> onLineUsers = Server.onLineSockets.values();
        for (Socket client : Server.onLineSockets.keySet()) {
            try {
                DataOutputStream dos = new DataOutputStream(client.getOutputStream());
                dos.writeInt(1);
                dos.writeInt(onLineUsers.size());
                for (String user : onLineUsers) {
                    dos.writeUTF(user);
                }
                dos.flush();
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }

    public void start() {
        run();
    }
}
